//
//  LastTipItemView.m
//  TipsProject
//
//  Created by Suny on 15/6/7.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "LastTipItemView.h"
#import "UIImageView+WebCache.h"

#import "TipInfoToolBar.h"
#import "SegmentView.h"
@interface UserPannel : MGBox
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation UserPannel

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		[self addSubview:self.nameLabel];
		[self addSubview:self.headerImageView];
	}

	return self;
}
- (UILabel *)nameLabel
{
	if (!_nameLabel)
	{
		_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.headerImageView.centerX,
															   self.headerImageView.top,
															   self.width - self.headerImageView.centerX - 20,
															   30)];
		_nameLabel.font = [UIFont systemFontOfSize:16];
		_nameLabel.backgroundColor = [UIColor whiteColor];

		
	}
	return _nameLabel;
}

- (UIImageView *)headerImageView
{
	
	if (!_headerImageView)
	{
		CGFloat imageSize = 60;
		
		_headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30,
																	   0,
																	   imageSize,
																	   imageSize)];
		_headerImageView.centerY = (CGFloat) (self.height/2.0);
		_headerImageView.layer.borderColor = [UIColor whiteColor].CGColor;
		_headerImageView.backgroundColor = [UIColor lightGrayColor];
		_headerImageView.layer.borderWidth = 2;
		_headerImageView.layer.cornerRadius = _headerImageView.width/2.0;
		_headerImageView.clipsToBounds = YES;
		
	}
	return _headerImageView;
}


@end

@implementation LastTipItemView
+ (LastTipItemView *)itemWithTipInfo:(TipInfo *)tipInfo withSize:(CGSize)size
{
	LastTipItemView *tipItemView = [LastTipItemView boxWithSize:size];
	tipItemView.contentMode = MGLayoutStackHorizontalWithWrap;
	tipItemView.tipInfo = tipInfo;
	// do the photo loading async, because internets
	__weak typeof(tipItemView) weakBox = tipItemView;
	tipItemView.asyncLayoutOnce = ^{
		[weakBox loadItemData];
	};
	
	return tipItemView;
}

- (void) loadItemData
{
	WEAKSELF
	dispatch_async(dispatch_get_main_queue(), ^{
		STRONGSELF
		
		UserPannel *panel = [UserPannel boxWithSize:CGSizeMake(strongSelf.width, 100)];
		panel.nameLabel.text = [NSString stringWithFormat:@"           %@", strongSelf.tipInfo.memberNickName];
		[panel.headerImageView setQNImageWithURL:[NSURL URLWithString:strongSelf.tipInfo.memberPhoto]
								 placeholderImage:[UIImage imageNamed:@"uv_default_avatar.png"]];
		[strongSelf.boxes addObject:panel];
		
		TipInfoToolBar *toolBar = [TipInfoToolBar tipInfoToolBarWithTipInfo:self.tipInfo
																	   andSize: CGSizeMake(strongSelf.width, 50)]  ;
		
		toolBar.backgroundColor = [UIColor whiteColor];
		[strongSelf.boxes addObject:toolBar];
		
		[strongSelf layout];
	});

	
}



@end
